package com.zx.job.controller;

import Job.Dto.JobDto;
import Job.Dto.SalaryDto;
import com.zx.job.mapper.JobRepository;
import com.zx.job.service.JobService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import pojo.Result;

import java.util.List;

@RestController
@RequestMapping("/jobs")
@RequiredArgsConstructor
public class JobController {
    private final JobService jobService;
    private final JobRepository jobRepository;

    @GetMapping("/getWorkSpaceById/{id}")
    public String getWorkSpaceById(@PathVariable int id) {
        return jobRepository.getWorkSpaceById(id);
    }
    @GetMapping("/findJobById/{id}")
    public JobDto findById(@PathVariable("id") int id) {
        return jobRepository.findById(id);
    }

    @PostMapping("/update")
    public void updateJob(@RequestBody JobDto jobDto) {
        System.out.println(jobDto);
        jobRepository.UpdateJob(jobDto);
    }

    @GetMapping("/delete/{id}")
    public void deleteJob(@PathVariable("id") int id) {
        jobRepository.deleteJob(id);
    }

    @GetMapping("/find/{companyId}")
    List<JobDto> findAllByCompanyId(@PathVariable("companyId") int companyId) {
        return jobRepository.findByCompanyId(companyId);
    }

    @GetMapping("/find/all")
    List<JobDto> findAlljol() {
        return jobRepository.findAll();
    }

    @Operation(summary = "获取所有岗位")
    @GetMapping()
    Result<List<JobDto>> findAll() {
        return Result.success(jobService.findAll());
    }

    @Operation(summary = "根据条件查询岗位")
    @GetMapping("/select")
    public Result<List<JobDto>> findByCriteria(
            @RequestParam(name = "categoryId", required = false) Integer categoryId,
            @RequestParam(name = "city", required = false) String city,
            @RequestParam(name = "salaryL", required = false) Integer salaryL,
            @RequestParam(name = "salaryR", required = false) Integer salaryR,
            @RequestParam(name = "degree", required = false) String degree
    ) {
        return Result.success(jobRepository.findByCriteria(categoryId, city, salaryL, salaryR, degree));
    }

    @Operation(summary = "获取所有薪资")
    @GetMapping("/getSalaries")
    Result<List<SalaryDto>> getSalaries() {
        List<SalaryDto> ans = jobRepository.findAllSalary();
        return Result.success(ans);
    }


    @Operation(summary = "添加岗位")
    @PostMapping("/insert")
    Result<?> insertJob(@RequestBody JobDto jobDto) {
        jobRepository.addJob(jobDto);
        return Result.success();
    }
}
